/**
 * 
 */
package com.facebookfetcher.main;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author piglet
 *
 */
public class GamesFacebookMain {	
	protected static final Log logger = LogFactory.getLog("GamesFacebookMain");
	protected static final FacebookDataSource fbDataSource= new FBDataSourceImpl();
	//protected static final FacebookDataSource fbDataSource= new FBDataSourceDummy();	
	private static final FacebookFetcher fbFetcher = new FacebookFetcher();
	private static final String MAIN_URL = "http://www.gamesfacebook.net/search.asp?sql=&ord=&sel=";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//fetch all title
		List<Request> RequestList = new ArrayList<Request>();
		for(int i=1; i<14;i++)
		{			
			try {
				URL url = new URL(MAIN_URL+i);
				RequestList.addAll(getLink(url));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		logger.info("update from :"+RequestList.size());		
		List<String> links = fbDataSource.getFetchLink(RequestList);								
		for(String link:links)
		{
			fbFetcher.fetchFBApp(link, false);
		}		

	}
	private static List<Request> getLink(URL url) {
		List<Request> request = new ArrayList<Request>();
		try {			
			HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
		    urlc.connect();
			InputStreamReader reader = new InputStreamReader(urlc.getInputStream());
			//InputStreamReader reader = new InputStreamReader(url.openStream(),"utf-8");
			Source source=new Source(reader);
			List<Element> items = source.getAllElementsByClass("col4");
			for(Element item:items)
			{
				Element title = item.getFirstElementByClass("title");				
				request.add(new Request(title.getTextExtractor().toString(), Request.Type.Name));
			}
			logger.debug("content:"+source.toString());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return request;
	}

}
